package org.jeecg.modules.article.job;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.article.constant.ArticleLikeConstant;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.article.mapper.ClickLikeMapper;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Set;

/**
 * TODO: redis点赞信息，异步存储到数据库中，每半小时一次（具体时间可以定）
 *
 * @author Zhang Hao yu
 * @date 2020/11/9 21:33
 */
@Slf4j
public class ArticleLikeSaveJob implements Job {
    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private ClickLikeMapper clickLikeMapper;

    /**
     * TODO: 异步存储点赞信息到数据库中
     *
     *
     * @author ZhangHaoyu
     * @param jobExecutionContext
     * @return void
     */
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info(String.format(" phil 点赞存储定时任务 ArticleLikeSaveJob !  时间:" + DateUtils.getTimestamp()));
        saveData();
    }
    /**
     * TODO: 遍历获取所需的值
     *
     *
     * @author ZhangHaoyu
     * @return void
     */
    private void saveData() {
        // 获取key
        Set<Object> articleIds = redisUtil.hKeys(ArticleLikeConstant.USER_LIKE_ARTICLE_KEY);
        // 获取对应的set集合
        articleIds.forEach(articleId->{
            Set<String> uids = (Set<String>) redisUtil.hget(ArticleLikeConstant.USER_LIKE_ARTICLE_KEY, articleId.toString());

            // 保存到数据库中  批量插入

        });
    }
}